package com.example.database.controller;

import com.example.database.entity.AdmissionForm;
import com.example.database.entity.ClassInstructor;
import com.example.database.entity.LeavingForm;
import com.example.database.entity.Student;
import com.example.database.service.ClassInstructorService;
import com.example.database.service.StudentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/class")
public class ClassInstructorController
{
    @Resource
    private ClassInstructorService classInstructorService;

    @Resource
    private StudentService studentService;

    @PostMapping(value = "/login")
    public boolean login(@RequestParam(value = "teacher_id") Integer teacher_id,
                         @RequestParam(value = "password") String password, HttpSession session)
    {
        return classInstructorService.login(teacher_id, password, session);
    }

    @GetMapping(value = "/admission_form")
    public List<AdmissionForm> getAdmissionForm(HttpSession session)
    {
        ClassInstructor classInstructor = (ClassInstructor) session.getAttribute("classInstructor");
        if (classInstructor == null) return null;
        return classInstructorService.findAdmissionFormByClass(classInstructor.getClass_id());
    }

    @GetMapping(value = "/leaving_form")
    public List<LeavingForm> getLeavingForm(HttpSession session)
    {
        ClassInstructor classInstructor = (ClassInstructor) session.getAttribute("classInstructor");
        if (classInstructor == null) return null;
        return classInstructorService.findLeavingFormByClass(classInstructor.getClass_id());
    }

    @PostMapping(value = "/admission_form/process")
    public boolean processAdmissionForm(@RequestParam(value = "form_id") Integer formId,
                                        @RequestParam(value = "operation") Boolean operation,
                                        @RequestParam(value = "reason") String reason)
    {
        return classInstructorService.processAdmissionFormByClassInstructor(formId, operation, reason);
    }

    @PostMapping(value = "/leaving_form/process")
    public boolean processLeavingForm(@RequestParam(value = "form_id") Integer formId,
                                      @RequestParam(value = "operation") Boolean operation,
                                      @RequestParam(value = "reason") String reason)
    {
        return classInstructorService.processLeavingFormByClassInstructor(formId, operation, reason);
    }

    @GetMapping(value = "/most_admission_student/class")
    List<Student> findMostAdmissionStudentByClass(@RequestParam(value = "classId") Integer classId)
    {
        return studentService.findMostAdmissionStudentByClass(classId);
    }
}
